package DataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import pojo.NguoiDungPOJO;

public class NguoiDungDA extends BaseDA {

	public NguoiDungPOJO get(String tenDangNhap) {

		String sql = "select * from nguoidung where TenDangNhap='" + tenDangNhap + "'";
		NguoiDungPOJO rs = null;
		
		try {
			ResultSet rst = provider.executeQuery(sql);
			while (rst.next()) {
				rs=getProperties(rst);
				break;
			}
		} catch (Exception e) {
			rs = null;
		}

		return rs;
	}
	public String getDeviceID(String tenDangNhap){
		String sql = "select DeviceID from nguoidung where tendangnhap='" + tenDangNhap+"'" ;
		String rs = "";		
		try {
			ResultSet rst = provider.executeQuery(sql);
			if (rst.next()) {
				rs=rst.getString("DeviceID");				
			}
		} catch (Exception e) {
			rs = "";
		}

		return rs;
	}
	public ArrayList<NguoiDungPOJO> get() {

		String sql = "select * from nguoidung";
		ArrayList<NguoiDungPOJO> rs = null;		
		try {
			ResultSet rst = provider.executeQuery(sql);
			rs = new ArrayList<NguoiDungPOJO>();
			while (rst.next()) {
				NguoiDungPOJO nd = getProperties(rst);
				rs.add(nd);
			}
		} catch (Exception e) {
			rs = null;
		}

		return rs;
	}

	public void updateDeiveID(String tenDangNhap, String regID) {
		String sql="update nguoidung set DeviceId='"+regID+"' where TenDangNhap='"+tenDangNhap+"'";		
		provider.executeUpdate(sql);		
	}
	public void resetDeviceID(String regID){
		String sql="update nguoidung set deviceid='reset' where deviceid='"+regID+"'";
		provider.executeUpdate(sql);
	}
	public void updateDeiveLastRequest(String tenDangNhap, long time) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = sdf.format(time);
		String sql="update nguoidung set LastRequest='"+currentTime+"' where TenDangNhap='"+tenDangNhap+"'";		
		provider.executeUpdate(sql);
	}
	public ArrayList<NguoiDungPOJO> getNguoiDungCuaNhom(int idNhom){
		ArrayList<NguoiDungPOJO> rs=null;
		String sql="select nd.* from nhom n join nhom_nguoidung nnd on n.Id=nnd.IdNhom join nguoidung nd on nd.TenDangNhap=nnd.NguoiDung where n.Id="+idNhom;
		
		try{
			rs=new ArrayList<NguoiDungPOJO>();
			ResultSet rss=provider.executeQuery(sql);
			while(rss.next()){
				NguoiDungPOJO nd = getProperties(rss);
				rs.add(nd);
			}
		}catch (Exception e) {
			rs=null;
		}
		
		return rs;
	}
	public ArrayList<NguoiDungPOJO> getNguoiDungCuaCay(int idCay){		
		int idNhom=new CayThaoLuanDA().get(idCay).getIdNhom();
		return getNguoiDungCuaNhom(idNhom);		
	}
	@Override
	protected NguoiDungPOJO getProperties(ResultSet rss) throws SQLException {
		NguoiDungPOJO rs=new NguoiDungPOJO();
		rs.setTenDangNhap(rss.getString("TenDangNhap"));
		rs.setMatKhau(rss.getString("MatKhau"));
		rs.setDeviceID(rss.getString("DeviceID"));
		rs.setId(rss.getInt("ID"));
		rs.setLastRequest(rss.getTimestamp("LastRequest").getTime());
		return rs;
	}
}
